package com.hdax.dm.controller;

import com.hdax.dm.common.utils.CommonResponse;
import com.hdax.dm.common.utils.ResponseUtil;
import com.hdax.dm.dto.ConfirmPayDto;
import com.hdax.dm.dtos.user.LinkUserDto;
import com.hdax.dm.entity.order.DmOrder;
import com.hdax.dm.service.LinkUserService;
import com.hdax.dm.service.OrderService;
import com.hdax.dm.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(path = "/p/order")
public class OrderController {

    @Autowired
    private LinkUserService linkUserService;
    @Autowired
    private OrderService orderService;

    @PostMapping(path = "ticketbuyerlist")
    public CommonResponse<List<LinkUserDto>> ticketBuyerList(HttpServletRequest request){
        //获取请求头 token
        String token = request.getHeader("token");
        //解析token获取当前登录用户编号
        Long userId = Long.parseLong(TokenUtil.parseToken(token).get("userId"));
        return linkUserService.ticketBuyerList(userId);
    }

    @PostMapping(path = "queryorderlist")
    public CommonResponse queryorderlist(@RequestBody Map<String,String> params, HttpServletRequest request){
        String keyword = params.get("keyword");
        String orderTime = params.get("orerTime");
        String orderType = params.get("orderType");
        return orderService.queryorderlist(keyword,orderType,orderTime,request.getHeader("token"));
    }

    @PostMapping(path = "confirmpay")
    public CommonResponse<ConfirmPayDto> confirmPay(@RequestBody Map<String,String> params){
        String orderNo = params.get("orderNo");
        DmOrder order = orderService.getOrder(orderNo);
        ConfirmPayDto dto = new ConfirmPayDto();
        dto.setOrderNo(order.getOrderNo());
        dto.setItemName(order.getItemName());
        dto.setTotalAmount(order.getTotalAmount().intValue());
        return ResponseUtil.returnSuccess(dto);
    }
}
